package com.ruoyi.web.controller.system;

import java.util.List;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.RegisterComuser;
import com.ruoyi.system.service.IRegisterComuserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.service.ISysUserService;

/**
 * 公司用户注册Controller
 * 
 * @author ruoyi
 * @date 2024-06-19
 */
@RestController
@RequestMapping("/system/comuser")
public class RegisterComuserController extends BaseController
{
    @Autowired
    private ISysUserService userService;

    @Autowired
    private IRegisterComuserService registerComuserService;

    @Autowired
    private ISysConfigService configService;

    /**
     * 查询公司用户注册列表
     */
    @PreAuthorize("@ss.hasPermi('system:comuser:list')")
    @GetMapping("/list")
    public TableDataInfo list(RegisterComuser registerComuser)
    {
        startPage();
        List<RegisterComuser> list = registerComuserService.selectRegisterComuserList(registerComuser);
        return getDataTable(list);
    }

    /**
     * 导出公司用户注册列表
     */
    @PreAuthorize("@ss.hasPermi('system:comuser:export')")
    @Log(title = "公司用户注册", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RegisterComuser registerComuser)
    {
        List<RegisterComuser> list = registerComuserService.selectRegisterComuserList(registerComuser);
        ExcelUtil<RegisterComuser> util = new ExcelUtil<RegisterComuser>(RegisterComuser.class);
        util.exportExcel(response, list, "公司用户注册数据");
    }

    /**
     * 获取公司用户注册详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:comuser:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(registerComuserService.selectRegisterComuserById(id));
    }

    /**
     * 新增公司用户注册
     */
    @PreAuthorize("@ss.hasPermi('system:comuser:add')")
    @Log(title = "公司用户注册", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RegisterComuser registerComuser)
    {
        return toAjax(registerComuserService.insertRegisterComuser(registerComuser));
    }

    /**
     * 公司用户注册
     */
    @PostMapping("/register")
    @PermitAll
    public AjaxResult register(@RequestBody RegisterComuser registerComuser)
    {
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser"))))
        {
            return error("当前系统没有开启注册功能！");
        }
        SysUser user=new SysUser();
        user.setUserName(registerComuser.getLoginName());
        user.setNickName(registerComuser.getResponsiblePerson());
        user.setPhonenumber(registerComuser.getPhonenumber());
        user.setEmail(registerComuser.getEmail());
        if (!userService.checkUserNameUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
        else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，手机号码已存在");
        }
        else if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
        String msg = registerComuserService.register(registerComuser);
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }

    /**
     * 修改公司用户注册
     */
    @PreAuthorize("@ss.hasPermi('system:comuser:edit')")
    @Log(title = "公司用户注册", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RegisterComuser registerComuser)
    {
        return toAjax(registerComuserService.updateRegisterComuser(registerComuser));
    }

    /**
     * 删除公司用户注册
     */
    @PreAuthorize("@ss.hasPermi('system:comuser:remove')")
    @Log(title = "公司用户注册", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(registerComuserService.deleteRegisterComuserByIds(ids));
    }
}
